Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_edit_name:false,
    uname:'',
    code:'',
    info:{face:''}
  },

  onShow(){
    this.load();
  },
  setInputNickname(e){
    if(e.detail.value.length==0){
      return;
    }
    this.setData({
      uname:e.detail.value
    })
  },
  setInput(e){
    let key=e.currentTarget.dataset.key;
    this.setData({
      [key]:e.detail.value
    })
  },
  setNamePop(){
    this.setData({
      show_edit_name:true,
      uname:''
    })
  },
  hideUname(){
    this.setData({
      show_edit_name:false,
      uname:''
    })
  },
  logout(){
    wx.util.ask("是否确认退出？",()=>{
      wx.removeStorageSync("user_token");
      wx.removeStorageSync("is_login");
      wx.switchTab({
        url:"/pages/my/my"
      })
    })
  },
  async setName(){
    if(this.data.uname==""){
      return;
    }
    this.setData({
      show_edit_name:false
    })
    await wx.util.post("user/set_name",{uname:this.data.uname},false);
    this.setData({
      'info.uname':this.data.uname,
      uname:""
    })
  },
  async load(){
    let res=await wx.util.post("user/profile",{},false);
    this.setData({
      info:res.data
    })
  },
  changeAvatar(e){
    this.upload(e.detail.avatarUrl);
  },
  async upload(e){
    let res=await wx.util.uploadFile("upload/index",e,false,{'category':'face'});
    await wx.util.post("user/set_face",{face:res.url},false);
    this.setData({
      'info.face':res.url
    })
  }
})